﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;


namespace MessengerClient.LibCOP
{
    public partial class libChatDialog : UserControl
    {
        public libChatDialog()
        {
            InitializeComponent();

        }

        //Gán địa chỉ mặc định
        string IP = "127.0.0.1";
        int PORT = 100;

        //Các luồng
        Thread thConnectToServer;

        //Biến dùng để gửi, nhận dữ liệu
        byte[] dlReceive = new byte[1024];
        byte[] dLSend = new byte[1024];

        TcpClient _client;
        public TcpClient Client
        {
            get { return _client; }
            set { _client = value; }
        }

        private void KetNoi()
        {
            try
            {
                //Thông báo đã kết nối tới máy chủ
                Client = new TcpClient();
                Client.Connect(IPAddress.Parse(IP), PORT);
                txtListMessage.Text += "Connected to server " + IP + ":" + PORT + "\r\n";

                while (true)
                {
                    try
                    {
                        btnSend.Enabled = true;
                        byte[] dlReceive = new byte[1024];
                        Client.GetStream().Read(dlReceive, 0, 1024);
                        txtListMessage.Text = txtListMessage.Text + Encoding.Unicode.GetString(dlReceive);
                        txtListMessage.Text = txtListMessage.Text + "\r\n";
                    }
                    catch
                    {
                        txtListMessage.Text = txtListMessage.Text + "\r\nConnection failed!";
                        btnSend.Enabled = false;
                        KetNoi();
                    }
                }
            }
            catch
            {
                txtListMessage.Text = txtListMessage.Text + "\r\nConnection failed!";
                btnSend.Enabled = false;
                KetNoi();
            }
        }

        private void btnSend_Click(object sender, EventArgs e)
        {
            byte[] dlSend = new byte[1024];
            dlSend = Encoding.Unicode.GetBytes(txtListMessage.Text);
            Client.GetStream().Write(dlSend, 0, dlSend.Length);
        }

        private void libChatDialog_Load(object sender, EventArgs e)
        {
            Client.Close();
            thConnectToServer.Abort();
            Application.Exit();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            IP = txtIP.Text;
            PORT = int.Parse(txtPort.Text);
            thConnectToServer = new Thread(KetNoi);
            thConnectToServer.IsBackground = true;//Khi thoát sẽ đóng luồng lại
            thConnectToServer.Start();
        }
    }
}
